﻿//using Microsoft.AspNetCore.Mvc.Razor;
//using Microsoft.Extensions.DependencyInjection;
//using SingleBlog.Core.Services;
//using System;
//using System.Collections.Generic;
//using Microsoft.AspNetCore.Http;

//namespace SingleBlog.UI
//{
//    internal class SingleBlogViewLocationExpandor : IViewLocationExpander
//    {
//        readonly IServiceScopeFactory _serviceScope;
//        public SingleBlogViewLocationExpandor(IServiceScopeFactory serviceScope)
//        {
//            _serviceScope = serviceScope;
//        }

//        public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
//        {
//            if (!context.PageName.IsNullOrWhiteSpace())
//            {
//                using (var scope = _serviceScope.CreateScope())
//                {
//                    var httpContext = scope.ServiceProvider.GetRequiredService<IHttpContextAccessor>();

//                    var template = httpContext.HttpContext.Items["template"];

//                    //    var locations = viewLocations.Select(m => m.Replace("{0}", $"{template}/{{0}}").Replace("Views", "Templates"));
//                    //    return locations;
//                    //return viewLocations;
//                    return (new string[] {
//                        $"/Areas/{{2}}/Views/{template}/{{1}}/{{0}}{RazorViewEngine.ViewExtension}",
//                        $"/Areas/{{2}}/Views/{template}/Shared/{{0}}{RazorViewEngine.ViewExtension}",
//                        $"/Areas/{{2}}/Views/Shared/{{0}}{RazorViewEngine.ViewExtension}",
//                        $"/Views/{template}/{{1}}/{{0}}{RazorViewEngine.ViewExtension}",
//                        $"/Views/{template}/Shared/{{0}}{RazorViewEngine.ViewExtension}",
//                        $"/Areas/{{2}}/Views/Default/{{1}}/{{0}}{RazorViewEngine.ViewExtension}",
//                        $"/Areas/{{2}}/Views/Default/Shared/{{0}}{RazorViewEngine.ViewExtension}",
//                        $"/Areas/{{2}}/Views/Shared/{{0}}{RazorViewEngine.ViewExtension}",
//                        $"/Views/Default/{{1}}/{{0}}{RazorViewEngine.ViewExtension}",
//                        $"/Views/Default/Shared/{{0}}{RazorViewEngine.ViewExtension}",
//                //$"/Templates/Shared/{{0}}{RazorViewEngine.ViewExtension}",
//                //$"/Templates/{template}/{{0}}{RazorViewEngine.ViewExtension}",
//                //$"/Templates/{template}/Shared/{{0}}{RazorViewEngine.ViewExtension}"
//            });
//                }
//            }
//            return viewLocations;
//        }

//        public void PopulateValues(ViewLocationExpanderContext context)
//        {
//            using (var scope = _serviceScope.CreateScope())
//            {
//                var httpContext = scope.ServiceProvider.GetRequiredService<IHttpContextAccessor>();

//                if (!httpContext.HttpContext.Items.TryGetValue("template", out object value))
//                {
//                    var template = scope.ServiceProvider.GetRequiredService<TemplateService>().GetCurrentTemplateAsync().Result;
//                    httpContext.HttpContext.Items["template"] = template.DirName;
//                }
//            }
//        }
//    }
//}
